package com.tools.common.utils;

import android.support.v7.app.AppCompatActivity;
import android.telecom.Call;

import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class PermissionManager {
    private static AppCompatActivity sActivity;
    private static boolean sGranted = false;

    public static void setActivity(AppCompatActivity act) {
        sActivity = act;
    }

    private static synchronized void setResult(boolean granted) {
        sGranted = granted;
    }

    private static synchronized boolean getResult() {
        return sGranted;
    }

    public static synchronized void check(final String permission, final Callback cb) {
        if(sActivity == null) {
            return;
        }

        RxPermissions rxPermissions = new RxPermissions(sActivity);
        rxPermissions.request(permission)
            .subscribe(new Consumer<Boolean>() {
                @Override
                public void accept(Boolean granted) throws Exception {
                    if(cb != null) {
                        cb.onResult(granted);
                    }
                }
            });

    }

    public interface Callback {
        void onResult(boolean granted);
    }
}
